﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml.Linq;
using System.Windows.Resources;      // StreamResourceInfo
using System.IO;


namespace DBSchemaViewer.BackEnd.ModelXML
{
    /// <summary>
    /// Core for handling Metadata, this class is a singleton and is not exposed outside the assembly.
    /// 
    /// This class is a singleton, we used a thread safe approach to manage it, case 4 of the following link:
    /// http://www.yoda.arachsys.com/csharp/singleton.html
    /// 
    /// 
    /// Ms Official documentation: 
    /// http://msdn.microsoft.com/en-us/library/ms998558.aspx
    /// 
    /// But the content of the class is not thread safe, if it needs to be use in a multithreaded environment
    /// you will need to add some locking.
    /// </summary>
    public class MetaDataStoreXML
    {
        /// <summary>
        /// Singleton...
        /// </summary>
        static readonly MetaDataStoreXML instance = new MetaDataStoreXML();

        // Root of the data XML tree
        private XElement _root = null;

        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static MetaDataStoreXML()
        {
        }

        /// <summary>
        /// We are using a singleton, let's read the XML metada just once 
        /// </summary>
        MetaDataStoreXML()
        {
            StreamResourceInfo sr = Application.GetResourceStream(
                new Uri("Resources/DBTypeRosetta.xml", UriKind.Relative));


            StreamReader sReader = new StreamReader(sr.Stream);

             string strContent = sReader.ReadToEnd();
            

             _root = XElement.Parse(strContent);

             sReader.Close();

            //?
            //sr.Stream.Close();
        }


        public static MetaDataStoreXML Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// Returns configured Database Engines (SQL Server, Oracle, My SQL)
        /// </summary>
        public XElement DBEnginesBranch
        {
            get
            {
                return _root.Element("dbEngines");
            }
        }


        /// <summary>
        /// Returns configured Database Types (int, nvarchar..)
        /// in MetaDataDAL we apply a filter to get the description
        /// for the active db engine
        /// </summary>
        public XElement DBTypesBranch
        {
            get
            {
                return _root.Element("dbtypes");
            }
        }


    }
}
